package com.information.manage.gyjc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.information.manage.basic.common.result.Result;
import com.information.manage.gyjc.mapper.GongxuMapper;
import com.information.manage.gyjc.mapper.GongyiMapper;
import com.information.manage.gyjc.mapper.RefGyGxMapper;
import com.information.manage.gyjc.model.enity.RefGyGx;
import com.information.manage.gyjc.model.query.GyGxQuery;
import com.information.manage.gyjc.model.vo.GxGyPage;
import com.information.manage.gyjc.model.vo.GxGyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gyjc/ref_gy_gx")
public class RefGyGxController {
    @Autowired
    private RefGyGxMapper refGyGxMapper;
    @Autowired
    private GongxuMapper gongxuMapper;
    @Autowired
    private GongyiMapper gongyiMapper;


    @PostMapping("/list")
    public Result list(@RequestBody GyGxQuery query) {
        QueryWrapper<RefGyGx> wrapper = new QueryWrapper<RefGyGx>()
                .eq(query.getGyId() != null, "gy_id", query.getGyId());
        Page<RefGyGx> refGyGxPage = refGyGxMapper.selectPage(new Page<>(query.getCurPage(), query.getSize()), wrapper);
        List<RefGyGx> refGyGxList = refGyGxPage.getRecords();
        // 利用stream流按照getGyId分组和GxOrder升序排序
        List<RefGyGx> sortedList = refGyGxList.stream()
                .sorted(Comparator.comparing(RefGyGx::getGxOrder))
                .collect(Collectors.groupingBy(RefGyGx::getGyId))
                .values().stream()
                .flatMap(List::stream)
                .collect(Collectors.toList());

        List<GxGyVo> gxGyVoList = new ArrayList<>();
        for (RefGyGx refGyGx : sortedList) {
            GxGyVo gxGyVo = new GxGyVo();
            gxGyVo.setRefGyGx(refGyGx);
            gxGyVo.setGongxu(gongxuMapper.selectById(refGyGx.getGxId()));
            gxGyVo.setGongyi(gongyiMapper.selectById(refGyGx.getGyId()));
            gxGyVoList.add(gxGyVo);
        }
        GxGyPage page = new GxGyPage();
        page.setTotal((int) refGyGxPage.getTotal());
        page.setSize((int) refGyGxPage.getSize());
        page.setPages((int) refGyGxPage.getPages());
        page.setCurrent((int) refGyGxPage.getCurrent());
        page.setRecords(gxGyVoList);
        return Result.ok(page);
    }

    @PostMapping("/add")
    public Result<String> addRefGyGx(@RequestBody RefGyGx refGyGx) {
        QueryWrapper<RefGyGx> wrapper = new QueryWrapper<RefGyGx>()
                .eq("gy_id", refGyGx.getGyId())
                .eq("gx_id", refGyGx.getGxId());
        RefGyGx one = refGyGxMapper.selectOne(wrapper);
        if (one != null) {
            return Result.fail("工序已在此工艺中存在！");
        }
        int res = refGyGxMapper.insert(refGyGx);
        if (res > 0) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }

    @PostMapping("/delete")
    public Result<String> deleteRefGyGx(@RequestBody RefGyGx refGyGx) {
        QueryWrapper<RefGyGx> wrapper = new QueryWrapper<RefGyGx>()
                .eq("gy_id", refGyGx.getGyId())
                .eq("gx_id", refGyGx.getGxId());
        int res = refGyGxMapper.delete(wrapper);
        if (res > 0) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }


}
